﻿

define(function(require,exports,module) {
    var myPub = require('./public');
    var scrollLoad = require('./scrollLoading');

    var oWindow = $(window);
    var resizeTimer = null;
    var goodsDateUrl = '/shop/goods/findGoodsByGroupName';//模拟数据
    var oGoodsListBox = $('.js_goodsList');

    var lazLoading = new myPub.lazLoadingImg();
    var oScrollBox = $('.js_scrollBox');
    var ocontainBox = $('.js_contain');

    var oLoading = $('.js_loadingBtn').eq(1) //加载按钮
    var oLoadingNull = $('.js_loadingBtn').eq(0) //没有更多选项

    var oErrPage = $('.js_errPageArea');//无数据的情况
    var ohaveData = $('.js_myGoods_show')//有数据

   // lazLoading.AddLazLoadingImg(oScrollBox[0]);
    module.exports = {
        init:function(){
            connectHtml.AjaxData(connectHtml.start, connectHtml.limit);
            indexSetInit.Init();
           // myPub.JudgeAppHref($('.js_errPageArea a'));
            //myPub.JudgeAppHref(oGoodsListBox.find('a'));
            oWindow.on('resize',function() {
                if (resizeTimer) {
                    clearTimeout(resizeTimer)
                }
                resizeTimer = setTimeout(function(){
                    indexSetInit.Init(true);
                }, 400);
            });
            scrollLoad.oScrollLoading.ScrollPage(function () {
                if(!connectHtml.bLoadOff) {
                    return;
                }

                // console.log( _this.pagearr)
                // 滚动底部页数增加 起始数据等于上次结束数据  新的结束等于页数*页码数
                connectHtml.start = connectHtml.limit*connectHtml.pagearr;
                connectHtml.pagearr++;
                connectHtml.AjaxData(connectHtml.start, connectHtml.limit);
                // 设置app产品跳转链接
               // myPub.JudgeAppHref($('.js_myGoods_show').find('.js_appHref'));
            });


        }
    };

    var indexSetInit = {
        Init: function () {
            var _this = this;
            myPub.JudgeAppHref(oGoodsListBox.find('a'));
        }
    };
    var connectHtml = {
        limit: 20,                       // 每次加载多少数据
        getData: [],                    // 数据记录
        pagearr: 1,                     // 当前页码
        pageCout: 0,                    // 页码总数
        start:0,                        // 开始数据
        end: 0,                         // 结束数据
        time_Scroll:null,               // 计时器
        bLoadOff: true,                 // 加载开关
        // 设置广告数据
        AjaxData: function (star,limit,fn) {
            var _this = this;

            G_JSON_POST(goodsDateUrl,{
                "groupName":"超级会员日",
                "start":_this.start,
                "limit":_this.limit
            },function (data) {
                if( data.status == "error") {
                    ohaveData.hide();
                    oErrPage.show();
                    return false;
                }
                //存储数据
                if( data.status == "success") {
                    var dataList = data.obj;
                    // 对应数据为0 return
                    if(dataList.length == 0 && _this.pagearr < 2) {
                        _this.bLoadOff = false;
                        ohaveData.hide();
                        oErrPage.show();
                        return
                    }
                    if( _this.pagearr >= 1) {
                        if(dataList.length == 0){
                            oLoadingNull.show();
                            oLoading.hide();
                            _this.bLoadOff = false;
                            return
                        }
                    }
                    if(_this.pagearr == 1){
                        return;
                    }

                    //拼接页面
                    _this.GoodsLiBoxHtml(dataList);
                    ohaveData.show();

                    if(dataList.length <  _this.limit) {
                        oLoading.hide();
                        oLoadingNull.show();
                        _this.bLoadOff = false;
                    }
                    data = null;
                    if(fn)fn();
                }
            }, function () {
                // start
            }, function () {
                // end
            });
        },


        // 正常的数据
        GoodsLiBoxHtml : function (data, fn) {
            var _this = this;
            var shtml = '';
            for(var i=0; i<data.length; i++) {
                var saleOutHtml = '';
                if(data[i].store!=undefined&&data[i].store!=null&&data[i].store <= 0){
                    saleOutHtml = '<div class="supperOver"></div>';
                }
                var memberDayPrice = '0.00';
                if(data[i].memberDayPrice!=null&&data[i].memberDayPrice!=undefined){
                    memberDayPrice =data[i].memberDayPrice;
                }
                var salePrice = '0.00';
                if(data[i].salePrice!=null&&data[i].salePrice!=undefined){
                    salePrice =data[i].salePrice;
                }
                if(data[i].memberDayPrice!=null&&data[i].memberDayPrice!=undefined){
                    memberDayPrice =data[i].memberDayPrice;
                }
                shtml +='<li>'+
                    '    <a class="BoxSelf" _href="' + data[i].goodsSn + '" >'+
                    '        <div class="productimg">'+
                    saleOutHtml+
                    '            <img class="js_lazyImg" data-echo="'+httpImageUrl+data[i].showImage+'" src="'+base+'/static_v4/image/loadImg.jpg">'+
                    '        </div>'+
                    '        <aside class="VBox Bflex1 suppertext">'+
                    '            <div class="Bflex1 supper_t_title">'+
                    '                <h4>'+data[i].name+'</h4>'+
                    '                <p>'+data[i].productName+'</p>'+
                    '            </div>'+
                    '            <div class="suppdates">'+
                    '                <span></span>'+
                    '                <span>￥'+memberDayPrice+'</span>'+
                    '                <span>￥'+salePrice+'</span>'+
                    '            </div>'+
                    '        </aside>'+
                    '    </a>'+
                    '</li>'
            }


            oGoodsListBox.append(shtml);
            lazLoading.DestroyLazLoadingImg();
            lazLoading.AddLazLoadingImg(oScrollBox[0]);
            var nclientHeight = oScrollBox.height();
            var ncontainHeight = ocontainBox.outerHeight(true);
            if(ncontainHeight<nclientHeight){
                _this.start = _this.page * _this.limit;
                _this.page++;
                _this.AjaxData(_this.start,_this.limit);
            }
            if(fn)fn();
            indexSetInit.Init();
        }



    };

    lazLoading.DestroyLazLoadingImg();
    lazLoading.AddLazLoadingImg(oGoodsListBox[0]);

})